<?php

/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

    //layout
    public $layout = "camera";
    public $uses = array('Customers');
    //set up helper
    public $helpers = array(
        'Session',
        'Html' => array('className' => 'BoostCake.BoostCakeHtml'),
        'Form' => array('className' => 'BoostCake.BoostCakeForm'),
        'Paginator' => array('className' => 'BoostCake.BoostCakePaginator'),
        'Common',
        'GoogleMap',
    );
    public $menu_active = '';
    //component
    public $components = array('Session', 'Cookie','Email',
        'Auth' => array(
            'loginAction' => array(
                'controller' => 'login',
                'action' => 'index'
            ),
            'loginRedirect' => array(
                'controller' => 'home',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'login',
                'action' => 'logout'
            ),
            'authError' => 'Did you really think you are allowed to see that?',
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'Customers',
                    'fields' => array('username' => 'CustId', 'password' => 'Pass'),
                    'passwordHasher' => array(
                        'className' => 'Simple',
                        'hashType' => 'md5'
                    )
                )
            )
        )
    );

    public function beforeRender() {
        parent::beforeRender();
        $this->set('menu_active', $this->menu_active);
    }

    public function beforeFilter() {
        parent::beforeFilter();
        App::import('Vendor', 'Common');
        $this->Cookie->httpOnly = true;
        if (!$this->Auth->loggedIn() && $this->Cookie->read('remember_me_cookie')) {
            $cookie = $this->Cookie->read('remember_me_cookie');
            debug($cookie); // no need to echo it, debug function does that already
            $this->loadModel('Customers'); // If the User model is not loaded already
            $user = $this->Customers->find('first', array(
                'conditions' => array(
                    'Customers.CustId' => $cookie['CustId'],
                    'Customers.Pass' => $cookie['Pass']
                )
            ));
            if ($user && !$this->Auth->login($user['Customers'])) {
                $this->redirect('/login/logout'); // destroy session &   cookie
            } else {
                $this->redirect($this->Auth->redirectUrl()); // redirect to Auth.redirect if it is set, else to Auth.loginRedirect ('/users/userhome') if it is set, else to /
            }
        }
//        if ($this->params['controller'] == 'admin') {
//
//            // Specify which controller/action handles logging in:
//            //AuthComponent::$sessionKey = 'Auth.Admin'; // solution from http://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session
//
//            $this->Auth->loginAction = array('controller' => 'admin', 'action' => 'login');
//            $this->Auth->loginRedirect = array('controller' => 'admin', 'action' => 'index');
//            $this->Auth->logoutRedirect = array('controller' => 'admin', 'action' => 'login');
//            $this->Auth->authenticate = array(
//                'Form' => array(
//                    'userModel' => 'Admins',
//                    'fields' => array('username' => 'AdminId', 'password' => 'Pass'),
//                    'passwordHasher' => array(
//                        'className' => 'Simple',
//                        'hashType' => 'md5'
//                    )
//                )
//            );
//            $this->Auth->allow('login');
//        }
//        if ($this->params['controller'] != 'admin') {
//            // Specify which controller/action handles logging in:
//            //AuthComponent::$sessionKey = 'Auth.Customer'; // solution from 
//            $this->Auth->loginAction = array('controller' => 'customer_login', 'action' => 'index');
//            $this->Auth->loginRedirect = array('controller' => 'Home', 'action' => 'index');
//            $this->Auth->logoutRedirect = array('controller' => 'customer_login', 'action' => 'logout');
//            $this->Auth->authenticate = array(
//                'Form' => array(
//                    'userModel' => 'Customers',
//                    'fields' => array('username' => 'CustId', 'password' => 'Pass'),
//                    'passwordHasher' => array(
//                        'className' => 'Simple',
//                        'hashType' => 'md5'
//                    )
//                )
//            );
//        }
    }

}
